﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using cmsservice.DAL;
using cmsservice.Entity;
namespace cmsservice.DAL
{
    public class XYVideoDAL
    {

        //public List<XY_web_survey_topic> GetExam(int topic_1, int topic_2)
        //{
        //    string sql = "select * from web_survey_topic where id in( @topic_1, @topic_2 )";
        //    SQLTools tool = new SQLTools();
        //    tool.AddSqlParameter("topic_1", System.Data.SqlDbType.Int, topic_1);
        //    tool.AddSqlParameter("topic_2", System.Data.SqlDbType.Int, topic_2);
        //    return SqlHelper.GetDataByColumnsLst<XY_web_survey_topic>(DBConn.Em_school, sql, tool.GetSqlParameters());
        //}

        //public List<XY_web_survey_key> GetExamDetail(int topic_1)
        //{
        //    string sql = "select * from web_survey_key where t_id =@topic_1 ";
        //    SQLTools tool = new SQLTools();
        //    tool.AddSqlParameter("topic_1", System.Data.SqlDbType.Int, topic_1);
        //    return SqlHelper.GetDataByColumnsLst<XY_web_survey_key>(DBConn.Em_school, sql, tool.GetSqlParameters());
        //}
        public int updateScore(long id, int pscore)
        {
            string sql = "update sohuvideo set pscore=ISNULL(pscore,0)+@pscore where id=@id";
            Tools tool = new Tools();
            tool.AddSqlParameter("id", System.Data.SqlDbType.BigInt, id);
            tool.AddSqlParameter("pscore", System.Data.SqlDbType.Int, pscore);
            return SqlHelper.ExecuteNonQuery(DBConn.Em_school, System.Data.CommandType.Text, sql, tool.GetSqlParameters());
        }

        public VideoItem GetOne(long id)
        {
            string sql = "select * from sohuvideo where (IsEnable=1 or IsEnable is null) and id=@id";
            Tools tool = new Tools();
            tool.AddSqlParameter("id", System.Data.SqlDbType.BigInt, id);
            return SqlHelper.GetDataByColumns<VideoItem>(DBConn.Em_school, sql, tool.GetSqlParameters());
        }

        public List<VideoItem> GetAll()
        {
            string sql = "select * from sohuvideo where IsEnable=1 or IsEnable is null order by lastModified desc";
            return SqlHelper.GetDataByColumnsLst<VideoItem>(DBConn.Em_school, sql, null);
        }

        public List<VideoItem> GetVideo(int day)
        {
            DateTime cDate = DateTime.Now.Date.AddDays(-day);
            string sql = "select * from sohuvideo where (IsEnable=1 or IsEnable is null)  and lastModified>@lastModified order by lastModified desc";
            Tools tool = new Tools();
            tool.AddSqlParameter("lastModified", System.Data.SqlDbType.DateTime, cDate);
            return SqlHelper.GetDataByColumnsLst<VideoItem>(DBConn.Em_school, sql, null);
        }
        public List<VideoItem> GetLstByLike(string item)
        {
            string sql = "select * from sohuvideo where (IsEnable=1 or IsEnable is null) and  tag like @item";
            Tools tool = new Tools();
            tool.AddSqlParameter("item", System.Data.SqlDbType.VarChar, "%" + item + "%");
            return SqlHelper.GetDataByColumnsLst<VideoItem>(DBConn.Em_school, sql, tool.GetSqlParameters());
        }

        public void UpdateDing(long id)
        {
            string sql = "update sohuvideo set Ding=ISNULL(Ding,0)+1 where id=@id";
            Tools tool = new Tools();
            tool.AddSqlParameter("id", System.Data.SqlDbType.BigInt, id);
            SqlHelper.ExecuteNonQuery(DBConn.Em_school, System.Data.CommandType.Text, sql, tool.GetSqlParameters());
        }

        public void UpdateCai(long id)
        {
            string sql = "update sohuvideo set Cai=ISNULL(Cai,0)+1 where id=@id";
            Tools tool = new Tools();
            tool.AddSqlParameter("id", System.Data.SqlDbType.BigInt, id);
            SqlHelper.ExecuteNonQuery(DBConn.Em_school, System.Data.CommandType.Text, sql, tool.GetSqlParameters());
        }

        public void UpdateClickCount(long id)
        {
            string sql = "update sohuvideo set ClickCount=ISNULL(ClickCount,0)+1 where id=@id";
            Tools tool = new Tools();
            tool.AddSqlParameter("id", System.Data.SqlDbType.BigInt, id);
            SqlHelper.ExecuteNonQuery(DBConn.Em_school, System.Data.CommandType.Text, sql, tool.GetSqlParameters());
        }

        public long[] GetCountById(long id)
        {
            string sql = "select ClickCount,Cai,Ding from sohuvideo where id=@id";
            Tools tool = new Tools();
            tool.AddSqlParameter("id", System.Data.SqlDbType.BigInt, id);
            DataTable tb = SqlHelper.ExecuteGetTable(DBConn.Em_school, CommandType.Text, sql, tool.GetSqlParameters());
            if (tb.Rows.Count > 0)
            {
                var row = tb.Rows[0];
                long[] arr = new long[3];
                long ding = 0;
                arr[0] = row["ding"] == DBNull.Value ? 0 : (long)row["ding"];
                arr[1] = row["cai"] == DBNull.Value ? 0 : (long)row["cai"];
                arr[2] = row["ClickCount"] == DBNull.Value ? 0 : (long)row["ClickCount"];
                return arr;
            }
            else
            {
                return null;
            }
        }

        public void Delete(long id)
        {
            string sql = "update SohuVideo set IsEnable=0,DelTime=GETDATE() where id=@id";
            Tools tool = new Tools();
            tool.AddSqlParameter("id", SqlDbType.BigInt, id);
            SqlHelper.ExecuteNonQuery(DBConn.Em_school, CommandType.Text, sql, tool.GetSqlParameters());
        }

        public List<VideoItem> GetLst(List<long> ids)
        {
            string sql = "select * from SohuVideo where (IsEnable=1 or IsEnable is null) and id in (";
            for (int i = 0; i < ids.Count; i++)
            {
                sql += ids[i] + ",";
            }
            sql = sql.TrimEnd(',');
            sql += ")";
            return SqlHelper.GetDataByColumnsLst<VideoItem>(DBConn.Em_school, sql, null);
        }
    }
}
